package nc.impl.hrppl.pjapplication;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.hrppl.util.QueryBudgetMakeDetailUtil;
import nc.itf.hrpbm.sjxm.sjxmh.ISjxmHVOService;
import nc.itf.hrppl.pjapplication.IUapbdFourProjectService;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.ct.uitl.ArrayUtil;
import nc.vo.hrppl.pjapplication.AggPjapplVO;
import nc.vo.hrppl.pjapplication.PJApplHVO;
import nc.vo.hrppl.pjapplication.PjapplPVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDouble;
import nc.vo.sjxm.sjxm.AggSjxmVO;
import nc.vo.sjxm.sjxm.SjxmBVO;
import nc.vo.sjxm.sjxm.SjxmHVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UapbdFourProjectServiceImpl implements IUapbdFourProjectService {
    private BaseDAO baseDAO;
    private ISjxmHVOService sjxmHVOService;
    @Override
    public void approveUpdateFourProject(AggPjapplVO[] vos) throws BusinessException {
        if(ArrayUtil.isEmpty(vos)) return;
        List<AggSjxmVO> uvos = new ArrayList<>();
        for(AggPjapplVO vo : vos) {
            List<AggSjxmVO> lists = this.handlerWriteBackSJXMAggVO(vo, "sqdapprove");
            if(lists == null || lists.isEmpty()) continue;
            uvos.addAll(lists);
        }
        if(uvos.isEmpty()) return;
        this.getSjxmHVOService().saveAggSjxmVO(uvos.toArray(new AggSjxmVO[uvos.size()]));
    }

    @Override
    public void UnApproveUpdateFourProject(AggPjapplVO[] vos) throws BusinessException {
        if(ArrayUtil.isEmpty(vos)) return;
        List<AggSjxmVO> uvos = new ArrayList<>();
        for(AggPjapplVO vo : vos) {
            List<AggSjxmVO> lists = this.handlerWriteBackSJXMAggVO(vo, "sqdunapprove");
            if(lists == null || lists.isEmpty()) continue;
            uvos.addAll(lists);
        }
        if(uvos.isEmpty()) return;
        this.getSjxmHVOService().saveAggSjxmVO(uvos.toArray(new AggSjxmVO[uvos.size()]));
    }

    /**
     * 处理回写四级项目信息
     * @return
     * @throws BusinessException
     */
    protected List<AggSjxmVO> handlerWriteBackSJXMAggVO(AggPjapplVO vo, String type) throws BusinessException {
        List<AggSjxmVO> lists = new ArrayList<>();
        ISuperVO[] cgsups = vo.getChildren(PjapplPVO.class);
        PJApplHVO hvo = vo.getParentVO();
        Object belongpro = hvo.getAttributeValue("belongpro");// 父项目
        Object def1 = hvo.getAttributeValue("def1");
        Object year = hvo.getAttributeValue("applyyear");
        if(belongpro == null)  return null;
        if(def1 == null || Integer.valueOf(def1.toString()) != 3) return null;
        if(ArrayUtil.isEmpty(cgsups)) return null;
        Map<String, UFDouble> datas = QueryBudgetMakeDetailUtil.getBudgetMakeDetailData(belongpro);
        UFDouble tzdata = datas.get("tzdata");
        UFDouble zzdata = datas.get("zzdata");
        HashMap<String, Object> param = this.getParentProjectAlls(belongpro,vo);
        if(param.isEmpty()) return null;
        for(ISuperVO cgsup : cgsups ) {
            PjapplPVO cgvo = (PjapplPVO)cgsup;
            Object pk_fourcgxm = cgvo.getAttributeValue("vbdef1");
            if(pk_fourcgxm == null) continue;
            AggSjxmVO sjAggVO = this.getSjxmHVOService().findAggSjxmVOByPk(pk_fourcgxm.toString());
            if(sjAggVO == null) continue; // 不存在四级项目
            SjxmHVO sjxmHVO = sjAggVO.getParentVO();
            sjxmHVO.setStatus(VOStatus.UPDATED);
            param.put("pk_fourcgxm", pk_fourcgxm);
            param.put("ysyear", year);
//            param.put("threeproname", hvo.getBillno());
//            param.put("threeprono", hvo.getPjname());
            param.put("threeprotzmoney", tzdata);
            param.put("kymoney", zzdata);
            param.put("threeprofinysmoney", zzdata);
            param.put("threeproysmoney", zzdata);
            ISuperVO[] bvos = sjAggVO.getChildren(SjxmBVO.class);
            if(ArrayUtil.isEmpty(bvos)) return null;
            for(ISuperVO bvo : bvos ) {
                Object ysyear = bvo.getAttributeValue("ysyear");
                if(year.toString().equals(ysyear)) {// 说明年度一直
                    bvo.setStatus(VOStatus.UPDATED);
                    bvo.setAttributeValue("oneprono",param.get("oneprono"));
                    bvo.setAttributeValue("oneproname",param.get("oneproname"));
                    bvo.setAttributeValue("twoprono",param.get("twoprono"));
                    bvo.setAttributeValue("twoproname",param.get("twoproname"));
                    bvo.setAttributeValue("threeprono",param.get("threeprono"));
                    bvo.setAttributeValue("threeproname",param.get("threeproname"));
                    bvo.setAttributeValue("modifyobj", "HOP");
                    bvo.setAttributeValue("bdef1", "Y");// 不管审批还是需要四级项目都已经被引用
                    if ("sqdapprove".equals(type)) {// 项目申请审批通过
                        bvo.setAttributeValue("budstate", "2"); // 预算状态
                        bvo.setAttributeValue("fourstate", "2");
                    }else if("sqdunapprove".equals(type)) {  //项目申请取消审批
                        bvo.setAttributeValue("budstate","2"); // 预算状态
                        bvo.setAttributeValue("fourstate","1");
                    }
                }else{continue;}
            }
            lists.add(sjAggVO);
        }
        return lists;
    }

    /**
     * 项目申请
     * 获取当前项目所以上级
     * @param belongpro
     * @return
     * @throws BusinessException
     */
    @SuppressWarnings("unchecked")
    protected HashMap<String, Object> getParentProjectAlls(Object belongpro,AggPjapplVO vo) throws  BusinessException{
        HashMap<String, Object> param = new HashMap<String,Object>();
        PJApplHVO hvo = vo.getParentVO();
        param.put("threeprono", hvo.getAttributeValue("billno"));
        param.put("threeproname", hvo.getAttributeValue("pjname"));
        String sql = "select def1 from bd_project where dr = 0 " +
                " and pk_project ='" + belongpro + "' ";
        Object obj = this.getBaseDAO().executeQuery(sql, new ColumnProcessor());
        if(obj == null ) return param;
        Integer i = Integer.valueOf(obj.toString());
        Object pk_project = belongpro;
        while(i > 0 ) {
            String isql = "select project_code,project_name,pk_parentpro from bd_project where dr = 0 " +
                    " and pk_project ='" + pk_project + "' ";
            Object iobj = this.getBaseDAO().executeQuery(isql, new MapProcessor());
            if(iobj ==  null) {
                i--;
                continue;
            }
            Map<String,Object> map  =  (Map<String,Object>)iobj;
            Object project_code = map.get("project_code");
            Object project_name = map.get("project_name");
            pk_project = map.get("pk_parentpro");
            if(i == 3) {
                param.put("threeprono", project_code);
                param.put("threeproname", project_name);

            }else if(i == 2) {
                param.put("twoprono", project_code);
                param.put("twoproname", project_name);

            }else if(i == 1) {
                param.put("oneprono", project_code);
                param.put("oneproname", project_name);
            }
            i--;
        }
        return  param;
    }

    public BaseDAO getBaseDAO() {
        return this.baseDAO == null ? this.baseDAO = new BaseDAO() : baseDAO;
    }

    public ISjxmHVOService getSjxmHVOService() {
        if(null == this.sjxmHVOService){
            this.sjxmHVOService = NCLocator.getInstance().lookup(ISjxmHVOService.class);
        }
        return this.sjxmHVOService;
    }
}
